import $ from 'jquery';

var Http = (function () {
    var __base_path__;

    function start(args) {
        var pipeline = new Pipeline();
        pipeline.deploy(args);
    }

    function Pipeline() {
        this.Method = {
            POST: "POST",
            GET: "GET",
            DELETE: "DELETE",
            PUT: "PUT"
        };
        this.Sync = {
            ASYNC: "ASYNC",
            SYNC: "SYNC"
        };
        this.ContentType = {
            JSON: "application/json"
        };
        this.header = {};


        this.send = function () {
            this.header.url = __base_path__ + this.header.url;
            if (this.header.type == this.Method.GET) {
                if (this.header.data != null && typeof this.header.data != "undefined") {
                    for (var key in  this.header.data) {
                        this.header.url += "&" + key + "=" + this.header.data[key];
                    }
                    this.header.url = this.header.url.replace(/&/, "?");
                }
            }
            if (this.header.contentType == this.ContentType.JSON) {
                this.header.data = JSON.stringify(this.header.data);
            }
            $.ajax(this.header);
        };

        this.url = function (value) {
            this.header.url = value;
        };

        this.contentType = function (value) {
            value = value.toUpperCase();
            if (value in this.ContentType) {
                this.header.contentType = this.ContentType[value];
                return true;
            }
            return false;
        };

        this.async = function (value) {
            value = value.toUpperCase();
            if (value in this.Sync) {
                if (value == this.Sync.SYNC) {
                    this.header.async = false;
                } else if (value == this.Sync.ASYNC) {
                    this.header.async = true;
                }
                return true;
            }
            return false;
        };

        this.method = function (value) {
            value = value.toUpperCase();
            if (value in this.Method) {
                if (value == this.Method.DELETE || value == this.Method.PUT) {
                    this.header.type = this.Method.POST;
                    if (this.header.data == null || typeof    this.header.data == "undefined") {
                        this.header.data = {};
                    }
                    this.header.data._method = value;
                } else {
                    this.header.type = value;
                }
                return true;
            }
            return false;
        };

        this.deploy = function (args) {
            if (args != null && typeof args != "undefined") {
                for (var i = 0, length = args.length; i < length; i++) {
                    var value = args[i];
                    if (value instanceof Function) {
                        //默认第一个函数为成功回调，第二个函数为失败回调
                        if (this.header.success == null || typeof  this.header.success == "undefined") {
                            this.header.success = value;
                        } else if (this.header.error == null || typeof  this.header.error == "undefined") {
                            this.header.error = value;
                        }
                    } else if (value instanceof Object) {
                        //默认对象为数据体，多个对象，对象属性会扩展
                        if (this.header.data == null || typeof  this.header.data == "undefined") {
                            this.header.data = value;
                        } else {
                            $.extend(this.header.data, value);
                        }
                    } else if (typeof value == "string") {
                        //解析请求方法和请求路径
                        this.method(value) || this.async(value) || this.contentType(value) || this.url(value);
                    }
                }
                this.send();
            } else {
                throw new Error("无效的请求");
            }
        }
    }

    return {
        request: function () {
            if (__base_path__ == null || typeof __base_path__ == "undefined") {
                throw  new Error("__base_path__ 未初始化");
                return;
            }
            var args = arguments;
            start(args);
        },
        set basePath(basePath) {
            if (__base_path__ == null || typeof __base_path__ == "undefined") {
                __base_path__ = basePath;
            }
        }
    }
})();
export default Http;